﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.PhieuTTCuaKH // VƯƠNG TRÍ LỢI
{
    public partial class frmQuanLy : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Khai báo biến

        bool checkClose = false;
        public Entities.PhieuTTCuaKH[] hienthi;
        private Entities.PhieuTTCuaKH phieuthanhtoankh;

        DateTime batdau;
        DateTime ketthuc;
        #endregion

        #region Delegate
        public void SelectData(string hanhdong)
        {
            try
            {
                dgvHienThi.DataSource = HienThi();
                hienthi = (Entities.PhieuTTCuaKH[])dgvHienThi.DataSource;

                if (hanhdong == Common.Constants.Insert)
                {
                    frmXuLy XuLyPhieuTTKH = new frmXuLy(Common.Constants.Insert);
                    XuLyPhieuTTKH.Select = new frmXuLy.SelectData(SelectData);
                    XuLyPhieuTTKH.Show();
                }
            }
            catch
            {}
        }
        #endregion

        #region Sự kiện của Windown

        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            try
            {

                this.Closing += new System.ComponentModel.CancelEventHandler(frmQuanLyPhieuThanhToanKH_Closing);
                this.WindowState = FormWindowState.Maximized;
                // Select firts row
                if (dgvHienThi.RowCount != 0)
                {
                    phieuthanhtoankh = hienthi[0];
                }

                // click chuột trái image
                btnThoat.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnThem.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnSua.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnLocDieuKien.MouseDown += new MouseEventHandler(MouseDown_Chung);
                btnIn.MouseDown += new MouseEventHandler(MouseDown_Chung);

                //
                dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
                dgvHienThi.MouseDoubleClick += new MouseEventHandler(dgvHienThi_MouseDoubleClick);

                // text changed
                txttimkiem.TextChanged += new EventHandler(txttimkiem_TextChanged);
                lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
            }
            catch
            { }
        }

        private void frmQuanLy_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                switch (e.KeyCode)
                {
                    case Keys.F2:
                        {
                            Them();
                            break;
                        }
                    case Keys.F3:
                        {
                            Sua(); 
                            break;
                        }
                    case Keys.F5:
                        {
                            LocDK();
                            break;
                        }
                    case Keys.Escape:
                        {
                            Thoat();
                            break;
                        }
                    case Keys.Enter:
                        {
                            Sua();
                            break;
                        }
                }
            }
            catch
            { }

        }
      
        #endregion

        #region from Quan ly phiêu thanh toán khach hàng

        public frmQuanLy()
        {
            try
            {
                InitializeComponent();
                dgvHienThi.DataSource = HienThi();
                hienthi = (Entities.PhieuTTCuaKH[])dgvHienThi.DataSource;
                HienThiList();
                Event();
            }
            catch
            {}
        }

        // When enter on datagridview
        private void Event()
        {
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
            rdbMaHoaDon.Checked = true;
        }
        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                for (int i = 0; i < hienthi.Length; i++)
                {
                    if (hienthi[i].MaPhieuTTCuaKH == dgvHienThi.CurrentRow.Cells["MaPhieuTTCuaKH"].Value.ToString())
                    {
                        phieuthanhtoankh = hienthi[i];
                        this.Sua();
                    }
                }
            }
        }

        public void frmQuanLyPhieuThanhToanKH_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                if (!checkClose)
                    if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        e.Cancel = checkClose;
                    }
                    else
                        e.Cancel = !checkClose;
                else
                {
                }
            }
            catch
            {}
        }
        
        public Entities.PhieuTTCuaKH[] HienThi()
        {
            try
            {
                Entities.PhieuTTCuaKH[] phieuttkh =Common.StaticVariable.InterfaceObject.SelectPhieuTTKHTheoDiaDiem(Entitystatic.ViewFile().MaKho);
               
                Entities.PhieuTTCuaKH[] temp = new Entities.PhieuTTCuaKH[phieuttkh.Length];
                int sl = 0;
                for (int i = 0; i < phieuttkh.Length; i++)
                {
                    if (phieuttkh[i].Deleted == false)
                    {
                        temp[sl] = phieuttkh[i];
                        sl++;
                    }
                }

                phieuttkh = new Entities.PhieuTTCuaKH[sl];
                for (int i = 0; i < phieuttkh.Length; i++)
                {
                    phieuttkh[i] = temp[i];
                }
                //lbTongBanGhi.Content = Common.Constants.TongSo + phieuttkh.Length;
                return phieuttkh;
            }
            catch { return new Entities.PhieuTTCuaKH[0]; }
        }

        private void HienThiList()
        {
            try
            {
                if (hienthi == null)
                {
                    Entities.PhieuTTCuaKH gitri = new Entities.PhieuTTCuaKH();
                    dgvHienThi.DataSource = gitri;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["HanhDong"].HeaderText = "STT";
                dgvHienThi.Columns["HanhDong"].Visible = true;
                dgvHienThi.Columns["MaPhieuTTCuaKH"].HeaderText = "Mã Phiếu TTKH";
                dgvHienThi.Columns["MaPhieuTTCuaKH"].Visible = true;
                dgvHienThi.Columns["NgayLap"].HeaderText = "Ngày Lập";
                dgvHienThi.Columns["NgayLap"].Visible = true;
                dgvHienThi.Columns["TenKhachHang"].HeaderText = "Tên Khách Hàng";
                dgvHienThi.Columns["TenKhachHang"].Visible = true;
                dgvHienThi.Columns["NguoiNop"].HeaderText = "Người Nộp";
                dgvHienThi.Columns["NguoiNop"].Visible = true;
                dgvHienThi.Columns["TongThanhToan"].HeaderText = "Tổng Thanh Toán";
                dgvHienThi.Columns["TongThanhToan"].Visible = true;
                dgvHienThi.Columns["GhiChu"].HeaderText = "Ghi chú";
                dgvHienThi.Columns["GhiChu"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            }
            catch
            { }
        }

        #endregion

        #region Check Permission
        public bool CheckPer(int hanhDong)
        {
            return Common.Utilities.Check(Common.FormName.PhieuTTKH, hanhDong);
        }
        #endregion

        #region Nghiệp vụ
        // thêm
        public void Them()
        {
            try
            {
                // check per
                if (!CheckPer(1))
                {
                    MessageBox.Show(Common.MSG.MSG10(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check loop
                
                    frmXuLy XuLyPhieuTTKH = new frmXuLy(Common.Constants.Insert);
                    XuLyPhieuTTKH.Select = new frmXuLy.SelectData(SelectData);
                    XuLyPhieuTTKH.Show();
            }
            catch
            {}
        }

        // Sửa
        public void Sua()
        {
            try
            {
                for (int i = 0; i < hienthi.Length; i++)
                {
                    if (hienthi[i].MaPhieuTTCuaKH == dgvHienThi.CurrentRow.Cells["MaPhieuTTCuaKH"].Value.ToString())
                    {
                        phieuthanhtoankh = hienthi[i];
                    }
                }
                // check per
                if (!CheckPer(0))
                {
                    MessageBox.Show(Common.MSG.MSG9(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check object
                if (phieuthanhtoankh == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe);
                    return;
                }

                    frmXuLy XuLyPhieuThanhToanKhachHang = new frmXuLy(phieuthanhtoankh, Common.Constants.Update);
                    XuLyPhieuThanhToanKhachHang.Select = new frmXuLy.SelectData(SelectData);
                    XuLyPhieuThanhToanKhachHang.Show();
                   dgvHienThi.DataSource = HienThi();
            }
            catch
            { }
        }

        // loc
        void XuLy()
        {
            ArrayList array = new ArrayList();
            foreach (Entities.PhieuTTCuaKH item in hienthi)
            {
                if (item.NgayLap.Date >= batdau.Date && item.NgayLap.Date <= ketthuc.Date)
                {
                    array.Add(item);
                }
            }
            hienthi = (Entities.PhieuTTCuaKH[])array.ToArray(typeof(Entities.PhieuTTCuaKH));
        }

        void LocDK()
        {
            try
            {
                GUI.LocDieuKien.frmLocDieuKien frm = new LocDieuKien.frmLocDieuKien();
                frm.ShowDialog();
                if (GUI.LocDieuKien.frmLocDieuKien.kiemtra)
                {
                    DateTime truoc1;
                    DateTime sau1;
                    try
                    {
                        truoc1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.truoc);
                        sau1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.sau);
                    }
                    catch
                    {
                        return;
                    }
                    batdau = truoc1;
                    ketthuc = sau1;
                    hienthi = HienThi();
                    XuLy();
                    dgvHienThi.DataSource = hienthi;
                    GUI.LocDieuKien.frmLocDieuKien.kiemtra = false;
                }
            }
            catch
            { }
        }

        //In
        void In()
        {
            try
            {
                for (int i = 0; i < hienthi.Length; i++)
                {
                    if (hienthi[i].MaPhieuTTCuaKH == dgvHienThi.CurrentRow.Cells["MaPhieuTTCuaKH"].Value.ToString())
                    {
                        phieuthanhtoankh = hienthi[i];
                    }
                }
                if (phieuthanhtoankh == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe);
                    return;
                }
                this.Enabled = false;
                Entities.ChiTietPhieuTTCuaKH[] ctpttckh = Common.StaticVariable.InterfaceObject.SelectChiTietPhieuTTKH(phieuthanhtoankh.MaPhieuTTCuaKH);
                Entities.KhachHang kh = new Entities.KhachHang();
                kh.MaKhachHang = phieuthanhtoankh.MaKhachHang;
                kh = Common.StaticVariable.InterfaceObject.Select_KhachHang(kh);
                // true là chi
                if (phieuthanhtoankh.LoaiPhieu)
                {
                    int sotang = 0;
                    Entities.BCPhieuThanhToanKhachHangPHIEUCHI[] bc = new Entities.BCPhieuThanhToanKhachHangPHIEUCHI[ctpttckh.Length];
                    foreach (Entities.ChiTietPhieuTTCuaKH item in ctpttckh)
                    {
                        bc[sotang] = new Entities.BCPhieuThanhToanKhachHangPHIEUCHI();
                        bc[sotang].NgayHD = phieuthanhtoankh.NgayLap.ToString("dd/MM/yyyy");
                        bc[sotang].SoHD = phieuthanhtoankh.MaPhieuTTCuaKH;
                        bc[sotang].STT = sotang + 1;
                        bc[sotang].ThanhToan = double.Parse(item.ThanhToan);
                        bc[sotang].TongTienHD = double.Parse(item.TongTienThanhToan);
                        bc[sotang].SoTienNo = double.Parse(item.SoNo);

                        sotang++;
                    }

                    BaoCao.frmBaoCaorpt frm = new BaoCao.frmBaoCaorpt(bc, kh, phieuthanhtoankh);
                    if (frm.kiemtra)
                    {
                        frm.ShowDialog();
                        frm.kiemtra = false;
                    }

                }
                else
                {
                    int sotang = 0;
                    Entities.BCPhieuThanhToanKhachHangPHIEUTHU[] bc = new Entities.BCPhieuThanhToanKhachHangPHIEUTHU[ctpttckh.Length];
                    foreach (Entities.ChiTietPhieuTTCuaKH item in ctpttckh)
                    {
                        bc[sotang] = new Entities.BCPhieuThanhToanKhachHangPHIEUTHU();
                        bc[sotang].NgayHoaDon = phieuthanhtoankh.NgayLap.ToString("dd/MM/yyyy");
                        bc[sotang].SoHoaDon = phieuthanhtoankh.MaPhieuTTCuaKH;
                        bc[sotang].STT = sotang + 1;
                        bc[sotang].ThanhToan = double.Parse(item.ThanhToan);
                        bc[sotang].TongTienHoaDon = double.Parse(item.TongTienThanhToan);
                        bc[sotang].SoTienNo = double.Parse(item.SoNo);
                        sotang++;
                    }
                    BaoCao.frmBaoCaorpt frm = new BaoCao.frmBaoCaorpt(bc, kh, phieuthanhtoankh);
                    if (frm.kiemtra)
                    {
                        frm.ShowDialog();
                        frm.kiemtra = false;
                    }
                }
                this.Enabled = true;
            }
            catch
            {
            }
        }

        public bool CheckConflicDelete()
        {
            try
            {
                Entities.PhieuTTCuaKH[] temp = HienThi();
                for (int i = 0; i < temp.Length; i++)
                {
                    if (phieuthanhtoankh.MaPhieuTTCuaKH == temp[i].MaPhieuTTCuaKH)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        // Thoát
        public void Thoat()
        {
            try
            {
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    checkClose = true;
                    this.Close();
                }
            }
            catch
            {}
        }

        #endregion

        #region Hiển thị listview

        // Event SelectChange of listview

        public void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                for (int i = 0; i < hienthi.Length; i++)
                {
                    if (hienthi[i].MaPhieuTTCuaKH == dgvHienThi.CurrentRow.Cells["MaPhieuTTCuaKH"].Value.ToString())
                    {
                        phieuthanhtoankh = hienthi[i];
                    }
                }
            }
            catch { }
        }

        //lvphieuchi_MouseDoubleClick
        public void dgvHienThi_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                Sua();
            }
            catch { }

        }

        #endregion

        #region Tìm kiếm phiếu thu
        public void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int soluong = 0;
                if (rdbMaHoaDon.Checked == true)
                {
                    for (int i = 0; i < hienthi.Length; i++)
                    {
                        int kiemtra = hienthi[i].MaPhieuTTCuaKH.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper());
                        if (kiemtra >= 0)
                        {
                            soluong++;
                        }
                    }
                    if (soluong == 0)
                    {
                        dgvHienThi.DataSource = new Entities.PhieuTTCuaKH[0];
                        //lbTongBanGhi.Content = Common.Constants.TongSo + "0";
                        return;
                    }
                    Entities.PhieuTTCuaKH[] hienthitheoid = new Entities.PhieuTTCuaKH[soluong];
                    soluong = 0;
                    for (int i = 0; i < hienthi.Length; i++)
                    {
                        int kiemtra = hienthi[i].MaPhieuTTCuaKH.ToString().ToUpper().IndexOf(txttimkiem.Text.ToUpper());
                        if (kiemtra >= 0)
                        {
                            hienthitheoid[soluong] = hienthi[i];
                            soluong++;
                        }
                    }
                    dgvHienThi.DataSource = hienthitheoid;


                }
            }
            catch
            {
            }
        }
        #endregion

        #region Mouse Down
        void MouseDown_Chung(object sender, MouseEventArgs e)
        {
            try
            {
                Button temp = (Button)sender;
                switch (temp.Name)
                {
                    case Common.Constants.btnThoat:
                        {
                            Thoat();
                            break;
                        }
                    case Common.Constants.btnThem:
                        {
                            Them();
                            break;
                        }
                    case Common.Constants.btnSua:
                        {
                            Sua();
                            break;
                        }
                    case Common.Constants.btnLocDieuKien:
                        {
                            LocDK();
                            break;
                        }
                    case Common.Constants.btnIn:
                        {
                            In();
                            break;
                        }
                    default: break;
                }
            }
            catch
            {
            }
        }
        #endregion
    }
}
